package com.alstudio.ui.module.account;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.module.xmpp.request.ALUiXmppRequestCreator;
import com.alstudio.pojo.ALXmppEvent;
import com.alstudio.ui.base.TitleBarActivity;
import com.alstudio.ui.module.main.MainActivity;
import com.alstudio.ui.module.user.ForgotPassActivity;
import com.alstudio.utils.android.keyboard.ALKeyBoardManager;
import com.alstudio.utils.android.net.ALConnectionManager;
import com.alstudio.view.toast.MyToastUtil;
import com.loovee.imaohu.R;

/**
 * 登陆界面
 * 
 * @author alonso lee
 * 
 */
public class LoginActivity extends TitleBarActivity {

	private final String TAG = "LoginActivity";

	private EditText eUsername;
	private EditText ePassword;
	private TextView tvForgetPwd;
	private Button btnLogin;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setupView(this, TAG);
	}

	@Override
	protected void setupView(Context context, String name) {
		// TODO Auto-generated method stub
		super.setupView(context, name);
		setTitleBarContentView(R.layout.login_layout);
		setCenterTextViewMessage(R.string.BtnLogin);
		eUsername = (EditText) findViewById(R.id.et_username);
		ePassword = (EditText) findViewById(R.id.et_password);
		tvForgetPwd = (TextView) findViewById(R.id.tv_forget_pwd);
		btnLogin = (Button) findViewById(R.id.button_login);
		setViewClickListener();
	}

	@Override
	protected void setViewClickListener() {
		// TODO Auto-generated method stub
		super.setViewClickListener();
		eUsername.setOnClickListener(this);
		ePassword.setOnClickListener(this);
		tvForgetPwd.setOnClickListener(this);
		btnLogin.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tv_forget_pwd:
			// 忘记密码
			Intent i = new Intent(this, ForgotPassActivity.class);
			startActivity(i);
			break;
		case R.id.button_login:
			// 登陆
			doLogin();
			break;
		}
	}

	/**
	 * 执行登陆逻辑
	 */
	/**
	 * 执行登录逻辑
	 */
	private void doLogin() {
		if (!ALConnectionManager.isNetWorkConnected()) {
			return;
		}

		if (eUsername.getText().toString().trim().length() == 0) {
			MyToastUtil.getInstance().showToastOnCenter(
					getString(R.string.TxtEnterIdHint));
			eUsername.requestFocus();
			return;
		}

		if (ePassword.getText().toString().trim().length() == 0) {
			// 请输入密码
			MyToastUtil.getInstance().showToastOnCenter(
					getString(R.string.TxtEnterPasswordHint));
			ePassword.requestFocus();
			return;
		}

		if (ePassword.getText().toString().trim().length() < 6) {
			MyToastUtil.getInstance().showToastOnCenter(
					getString(R.string.TxtPasswordTooShort));
			ePassword.requestFocus();
			return;
		}
		ALKeyBoardManager.dismissKeyBoard(this);

		ALUiXmppRequestCreator.postALUiXmppLoginEvent(eUsername.getText()
				.toString(), ePassword.getText().toString());

		setLoadingProgressCancelAble(false);
		showLoadingProgress();
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#finish()
	 */
	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
		disableReceiveALXmppEvent();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.ui.base.BaseActivity#handleLoginedEvent(com.alstudio.pojo
	 * .ALLocalData)
	 */
	@Override
	public void handleLoginedEvent(ALXmppEvent data) {
		// TODO Auto-generated method stub
		super.handleLoginedEvent(data);
	}
	
	/* (non-Javadoc)
	 * @see com.alstudio.ui.base.BaseActivity#handleGetUserProfileEvent(com.alstudio.pojo.ALXmppEvent)
	 */
	@Override
	public void handleGetUserProfileEvent(ALXmppEvent data) {
		// TODO Auto-generated method stub
		super.handleGetUserProfileEvent(data);
		dismissLoadingProgress();
		Intent i = new Intent(this, MainActivity.class);
		startActivity(i);
		finish();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.ui.base.BaseActivity#handleAuthFailedEvent(java.lang.String)
	 */
	@Override
	public void handleAuthFailedEvent(String errorMessage) {
		// TODO Auto-generated method stub
		super.handleAuthFailedEvent(errorMessage);
		dismissLoadingProgress();
		MyToastUtil.getInstance().showCenterToastOnCenter(R.string.TxtSnsLoginAuthFail);
		ALLocalEnv.getLocalEnv().getMyAccount().setUserName("");
		ALLocalEnv.getLocalEnv().getMyAccount().setPassWord("");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.ui.base.BaseActivity#handleConnectionLostEvent(java.lang
	 * .String)
	 */
	@Override
	public void handleConnectionLostEvent(String errorMessage) {
		// TODO Auto-generated method stub
		super.handleConnectionLostEvent(errorMessage);
		dismissLoadingProgress();
		ALLocalEnv.getLocalEnv().getMyAccount().setUserName("");
		ALLocalEnv.getLocalEnv().getMyAccount().setPassWord("");
	}

}
